<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
    <title>${edit ? '编辑文档' : '创建文档'} - 知识库</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f5f5;
            color: #1a1a1a;
            height: 100vh;
            overflow: hidden;
        }
        
        .editor-container {
            display: flex;
            flex-direction: column;
            height: 100vh;
        }
        
        .editor-header {
            background: white;
            border-bottom: 1px solid #e5e5e5;
            padding: 16px 24px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            z-index: 100;
        }
        
        .header-left {
            display: flex;
            align-items: center;
            gap: 16px;
        }
        
        .back-btn {
            color: #666;
            text-decoration: none;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 6px;
            transition: color 0.3s ease;
        }
        
        .back-btn:hover {
            color: #1a1a1a;
        }
        
        .document-title {
            font-size: 18px;
            font-weight: 600;
            color: #1a1a1a;
        }
        
        .header-right {
            display: flex;
            align-items: center;
            gap: 12px;
        }
        

        
        .lock-indicator {
            background: #e3f2fd;
            color: #1565c0;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .btn {
            padding: 8px 16px;
            border-radius: 6px;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
            border: none;
            cursor: pointer;
            font-size: 14px;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }
        
        .btn-primary {
            background: #1a1a1a;
            color: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
        }
        
        .btn-primary:hover {
            background: #333;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        }
        
        .btn-secondary {
            background: white;
            color: #1a1a1a;
            border: 2px solid #e5e5e5;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        
        .btn-secondary:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
            border-color: #333;
        }
        
        .btn-danger {
            background: #d32f2f;
            color: white;
            box-shadow: 0 2px 8px rgba(211, 47, 47, 0.3);
        }
        
        .btn-danger:hover {
            background: #b71c1c;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(211, 47, 47, 0.4);
        }
        
        .editor-content {
            flex: 1;
            display: flex;
            background: white;
        }
        
        .editor-sidebar {
            width: 300px;
            background: #f8f8f8;
            border-right: 1px solid #e5e5e5;
            padding: 24px;
            overflow-y: auto;
        }
        
        .sidebar-section {
            margin-bottom: 24px;
        }
        
        .sidebar-title {
            font-size: 14px;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 12px;
        }
        
        .form-group {
            margin-bottom: 16px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 6px;
            color: #1a1a1a;
            font-weight: 500;
            font-size: 13px;
        }
        
        .form-group input,
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 8px 12px;
            border: 2px solid #e5e5e5;
            border-radius: 6px;
            font-size: 14px;
            transition: all 0.3s ease;
            background: white;
            color: #1a1a1a;
        }
        
        .form-group input:focus,
        .form-group select:focus,
        .form-group textarea:focus {
            outline: none;
            border-color: #333;
            box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.1);
        }
        
        .form-group textarea {
            min-height: 80px;
            resize: vertical;
        }
        
        .editor-main {
            flex: 1;
            display: flex;
            flex-direction: column;
        }
        
        .toolbar {
            background: #f8f8f8;
            border-bottom: 1px solid #e5e5e5;
            padding: 12px 16px;
            display: flex;
            align-items: center;
            gap: 8px;
            flex-wrap: wrap;
        }
        
        .toolbar-btn {
            padding: 6px 10px;
            border: 1px solid #e5e5e5;
            background: white;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 4px;
        }
        
        .toolbar-btn:hover {
            background: #f0f0f0;
            border-color: #333;
        }
        
        .toolbar-btn.active {
            background: #1a1a1a;
            color: white;
            border-color: #1a1a1a;
        }
        
        .toolbar-separator {
            width: 1px;
            height: 20px;
            background: #e5e5e5;
            margin: 0 8px;
        }
        
        .editor-area {
            flex: 1;
            padding: 24px;
            overflow-y: auto;
        }
        
        .title-input {
            width: 100%;
            font-size: 24px;
            font-weight: 600;
            border: none;
            outline: none;
            background: transparent;
            color: #1a1a1a;
            margin-bottom: 24px;
            padding: 0;
        }
        
        .title-input::placeholder {
            color: #999;
        }
        
        .content-editor {
            width: 100%;
            min-height: 500px;
            border: none;
            outline: none;
            background: transparent;
            color: #1a1a1a;
            font-size: 16px;
            line-height: 1.6;
            resize: none;
            font-family: inherit;
        }
        
        .content-editor::placeholder {
            color: #999;
        }
        
        .error-message {
            background: #ffebee;
            color: #c62828;
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #f44336;
            font-size: 14px;
        }
        
        .success-message {
            background: #e8f5e8;
            color: #2e7d32;
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #4caf50;
            font-size: 14px;
        }
        

        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .editor-sidebar {
                width: 100%;
                height: auto;
                border-right: none;
                border-bottom: 1px solid #e5e5e5;
            }
            
            .editor-content {
                flex-direction: column;
            }
            
            .header-right {
                flex-direction: column;
                gap: 8px;
            }
        }
    </style>
</head>
<body>
    <div class="editor-container">
        <div class="editor-header">
            <div class="header-left">
                <a href="/user/docs" class="back-btn">
                    <span>←</span>
                    返回文档列表
                </a>
                <div class="document-title">
                    ${edit ? '编辑文档' : '创建文档'}
                </div>
            </div>
            
            <div class="header-right">
                <c:if test="${edit && document.lockedBy != null && document.lockedBy != user.id}">
                    <div class="lock-indicator">
                        🔒 文档被 ${document.lockedByUsername} 锁定
                    </div>
                </c:if>
                
                <c:if test="${edit}">
                    <a href="/document/export/${document.id}" class="btn btn-primary">
                        📄 导出
                    </a>
                    <form action="/document/delete/${document.id}" method="post" style="display:inline;">
                        <button type="submit" class="btn btn-danger" onclick="return confirm('确定要删除这个文档吗？此操作不可恢复。')">
                            🗑️ 删除
                        </button>
                    </form>
                </c:if>
            </div>
        </div>
        
        <div class="editor-content">
            <div class="editor-sidebar">
                <div class="sidebar-section">
                    <div class="sidebar-title">文档信息</div>
                    <form:form modelAttribute="documentForm" id="createForm" method="post">
                        
                    </form:form>
                    <div class="form-group">
                        <label for="visibilitySelect">可见性</label>
                        <select id="visibilitySelect" name="isPublic" form="${edit ? 'editForm' : 'createForm'}">
                            <option value="false" ${!document.isPublic ? 'selected' : ''}>私有</option>
                            <option value="true" ${document.isPublic ? 'selected' : ''}>公开</option>
                        </select>
                    </div>
                </div>
                
                <div class="sidebar-section">
                    <div class="sidebar-title">文档统计</div>
                    <div style="font-size: 13px; color: #666;">
                        <div>字符数: <span id="charCount">0</span></div>
                        <div>字数: <span id="wordCount">0</span></div>
                        <c:if test="${edit}">
                            <div>创建时间: ${fn:substring(document.createdAt, 0, 19)}</div>
                            <div>更新时间: <span id="updateTime">${fn:substring(document.updatedAt, 0, 19)}</span></div>
                        </c:if>
                    </div>
                </div>
            </div>
            
            <div class="editor-main">
                <div class="toolbar">
                    <button type="button" class="toolbar-btn" onclick="formatText('bold')">
                        <strong>B</strong>
                    </button>
                    <button type="button" class="toolbar-btn" onclick="formatText('italic')">
                        <em>I</em>
                    </button>
                    <button type="button" class="toolbar-btn" onclick="formatText('underline')">
                        <u>U</u>
                    </button>
                    
                    <div class="toolbar-separator"></div>
                    
                    <button type="button" class="toolbar-btn" onclick="insertText('# ')">
                        H1
                    </button>
                    <button type="button" class="toolbar-btn" onclick="insertText('## ')">
                        H2
                    </button>
                    <button type="button" class="toolbar-btn" onclick="insertText('### ')">
                        H3
                    </button>
                    
                    <div class="toolbar-separator"></div>
                    
                    <button type="button" class="toolbar-btn" onclick="insertText('- ')">
                        列表
                    </button>
                    <button type="button" class="toolbar-btn" onclick="insertText('1. ')">
                        编号
                    </button>
                    
                    <div class="toolbar-separator"></div>
                    
                    <button type="button" class="toolbar-btn" onclick="insertText('```\n\n```')">
                        代码块
                    </button>
                    <button type="button" class="toolbar-btn" onclick="insertText('> ')">
                        引用
                    </button>
                </div>
                
                <div class="editor-area">
                    <c:if test="${not empty error}">
                        <div class="error-message">${error}</div>
                    </c:if>
                    
                    <c:if test="${not empty msg}">
                        <div class="success-message">${msg}</div>
                    </c:if>
                    
                    <c:choose>
                        <c:when test="${edit}">
                            <form id="editForm" action="/document/edit/${document.id}" method="post">
                                <input type="text" 
                                       id="titleInput" 
                                       name="title"
                                       class="title-input" 
                                       placeholder="请输入文档标题..." 
                                       value="${document.title}"
                                       ${edit && document.lockedBy != null && document.lockedBy != user.id ? 'readonly' : ''}
                                       required>
                                
                                <textarea id="contentEditor" 
                                          name="content"
                                          class="content-editor" 
                                          placeholder="开始编写您的文档内容..."
                                          ${edit && document.lockedBy != null && document.lockedBy != user.id ? 'readonly' : ''}
                                          required>${document.content}</textarea>
                                
                                <div style="margin-top: 32px; text-align: center;">
                                    <button type="submit" class="btn btn-primary" style="padding: 16px 48px; font-size: 18px; box-shadow: 0 2px 8px #0002;">
                                        💾 保存文档
                                    </button>
                                </div>
                            </form>
                        </c:when>
                        <c:otherwise>
                            <form id="createForm" action="/document/create" method="post">
                                <div class="form-group">
                                    <label for="spaceSelect">所属空间</label>
                                    <select id="spaceSelect" name="spaceId">
                                        <option value="">个人文档</option>
                                        <c:forEach var="space" items="${userSpaces}">
                                            <option value="${space.id}" ${(selectedSpaceId == space.id) ? 'selected' : ''}>
                                                ${space.name}
                                            </option>
                                        </c:forEach>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="visibilitySelect">可见性</label>
                                    <select id="visibilitySelect" name="isPublic">
                                        <option value="false">私有</option>
                                        <option value="true">公开</option>
                                    </select>
                                </div>
                                <input type="text" 
                                       id="titleInput" 
                                       name="title"
                                       class="title-input" 
                                       placeholder="请输入文档标题..." 
                                       required>
                                
                                <textarea id="contentEditor" 
                                          name="content"
                                          class="content-editor" 
                                          placeholder="开始编写您的文档内容..."
                                          required></textarea>
                                
                                <div style="margin-top: 32px; text-align: center;">
                                    <button type="submit" class="btn btn-primary" style="padding: 16px 48px; font-size: 18px; box-shadow: 0 2px 8px #0002;">
                                        💾 创建文档
                                    </button>
                                </div>
                            </form>
                        </c:otherwise>
                    </c:choose>
                </div>
            </div>
        </div>
    </div>
    

    
    <script>
        // 更新字数统计
        function updateWordCount() {
            const content = document.getElementById('contentEditor').value;
            const charCount = content.length;
            const wordCount = content.trim() === '' ? 0 : content.trim().split(/\s+/).length;
            
            document.getElementById('charCount').textContent = charCount;
            document.getElementById('wordCount').textContent = wordCount;
        }
        
        // 格式化文本
        function formatText(command) {
            const editor = document.getElementById('contentEditor');
            const start = editor.selectionStart;
            const end = editor.selectionEnd;
            const selectedText = editor.value.substring(start, end);
            
            let formattedText = '';
            switch (command) {
                case 'bold':
                    formattedText = `**${selectedText}**`;
                    break;
                case 'italic':
                    formattedText = `*${selectedText}*`;
                    break;
                case 'underline':
                    formattedText = `__${selectedText}__`;
                    break;
            }
            
            editor.value = editor.value.substring(0, start) + formattedText + editor.value.substring(end);
            editor.focus();
            editor.setSelectionRange(start + formattedText.length, start + formattedText.length);
            updateWordCount();
        }
        
        // 插入文本
        function insertText(text) {
            const editor = document.getElementById('contentEditor');
            const start = editor.selectionStart;
            const end = editor.selectionEnd;
            
            editor.value = editor.value.substring(0, start) + text + editor.value.substring(end);
            editor.focus();
            editor.setSelectionRange(start + text.length, start + text.length);
            updateWordCount();
        }
        
        // 页面加载时初始化字数统计
        document.addEventListener('DOMContentLoaded', function() {
            updateWordCount();
            
            // 监听内容变化
            document.getElementById('contentEditor').addEventListener('input', function() {
                updateWordCount();
            });
        });
    </script>
</body>
</html> 